import api from 'api'
import moment from 'moment'
import store from 'store'

export default{
  namespaced:true,
  state:{
    meal_id:"", //套餐id
    courseDate:{
      title:''
    },
    courseTime:{
      title:''
    },
    list:{ //套餐详情的参数
      guest_price:'暂无价格', //嘉宾价
      alliance_price:'暂无价格', //会员价
      pricecontain:'暂无', //价格包含
      subscribeinfo:'暂无', //预订说明
      unsubscribeinfo:'暂无', //退订说明
      content:'暂无', //套餐详情说明
      status:2 //2刚进入时状态 
    },
    price:{

    },
    num:1 //人数
  },
  mutations:{
    setData(state,res){ //设置套餐时间
      state.courseDate = res.date
      state.courseTime = res.time
      this.dispatch("meal/getMealData")
    },
    changeNum(state,num){
      state.num = num
    },
    sumPrice(state){
      let isvip = store.state.user.userInfo.member.isvip
      let price = {}
      //state.price.meal_price = isvip==1?state.list.alliance_price:state.list.guest_price //暂无会员
      price.meal_price = state.list.guest_price*state.num
      price.server_price = state.list.serviceprice*state.num
      price.total_price = parseFloat(price.meal_price) + parseFloat(price.server_price)
      state.price = price
      // console.log(state.price)
    }
  },
  actions:{
    getMealDetail(context,meal_id){  //套餐详情
      return new Promise((resolve, reject) => {
        context.state.meal_id = meal_id //保存套餐id
        sessionStorage.setItem("meal_id",meal_id)
        let params = {
          product_id:meal_id,
          courseid:store.state.common.courseid
        }
        api.get("/course/courseProductDetail",params).then(e=>{
          context.state.list = e.data
          context.state.list.status = 2
          // let meal_teetime = JSON.parse(sessionStorage.getItem("meal_teetime"))
          // if(meal_teetime){
          //   context.commit("setData",meal_teetime)
          // }
          resolve()
        })
      });
    },
    getMealData(context){
      let params = {
        courseid:store.state.common.courseid,
        type:2,
        teetime:context.state.courseDate.date + Number(context.state.courseTime.time)*3600,
        product_id:context.state.meal_id
      }
      api.get("/course/courseByDate",params).then(e=>{
        context.state.list = e.data
        context.state.list.status = 1
        context.commit("sumPrice")
      }).catch(err=>{
        if(err.errcode == "40012" || err.errcode == "40015"){
          context.state.list.status = 0
        }
      })
    },
    
  }
}